//
//  SEGTarget_Lottery.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/3/24.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGTarget_Lottery.h"

#import "SEGAwardAdView.h"
#import "SEGScratchCardView.h"
#import "SEGRedWalletView.h"
//typedef void (^SEGAwardRedWalletViewCloseCallBack)();

@implementation SEGTarget_Lottery

-(void)segAction_dealWithCheckActivityResultDict:(NSDictionary*)params{
    NSDictionary *dicData = [[params allKeys]containsObject:@"dicData"] ? [params valueForKey:@"dicData"]: [NSDictionary dictionary];
    SEGAwardForwardType frowardType = [[params allKeys]containsObject:@"frowardType"] ? (SEGAwardForwardType)[[params valueForKey:@"frowardType"] integerValue]: SEGAwardForwardType_AD;
    UINavigationController *nav = [[params allKeys]containsObject:@"nav"] ? [params valueForKey:@"nav"]: nil;
    SEGRedWalletFromType fromType = [[params allKeys]containsObject:@"fromType"] ? (SEGRedWalletFromType)[[params valueForKey:@"fromType"] integerValue]: SEGRedWalletFromType_OpenDoor;
    UIViewController *vc = [[params allKeys]containsObject:@"vc"] ? [params valueForKey:@"vc"]: nil;
    [self dealWithCheckActivityResultDict:dicData frowardType:frowardType navigation:nav fromType:fromType vc:vc];
}

-(void)segAction_showRedWalletViewWithType:(NSDictionary*)params{
    UINavigationController *nav = [[params allKeys]containsObject:@"nav"] ? [params valueForKey:@"nav"]: nil;
    SEGRedWalletFromType fromType = [[params allKeys]containsObject:@"fromType"] ? (SEGRedWalletFromType)[[params valueForKey:@"fromType"] integerValue]: SEGRedWalletFromType_OpenDoor;
    [self showRedWalletViewWithType:fromType navigation:nav];
}

- (void)dealWithCheckActivityResultDict:(NSDictionary *)dicData frowardType:(SEGAwardForwardType)frowardType navigation:(UINavigationController *)nav fromType:(SEGRedWalletFromType)fromType vc:(UIViewController*)vc{
    switch (frowardType) {
        case SEGAwardForwardType_AD://广告
        {
            //开门广告弹窗
            SEGAwardAdView *scratchView = [[SEGAwardAdView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            [scratchView updateViewWithDict:dicData callBack:^{
                //把门禁列表界面消失
                [vc.navigationController dismissViewControllerAnimated:NO completion:nil];
            }];
            [[[UIApplication sharedApplication] keyWindow] addSubview:scratchView];
            break;
        }
        case SEGAwardForwardType_Web://网页形式
        {
            NSDictionary *params = @{@"linkURL":[dicData valueForKey:@"forwardUrl"]};
            UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWebViewLoadController:params];
            VC.title = @"抽奖";
            [vc.navigationController pushViewController:VC animated:YES];
            break;
        }
        case SEGAwardForwardType_RedWallet://普通红包
        {
            //[self showRedWalletViewWithType:fromType navigation:nav closeCallback:^{}];
            [self showRedWalletViewWithType:fromType navigation:nav];
            break;
        }
        case SEGAwardForwardType_Scratch://刮奖
        {
            //开门刮奖
            SEGScratchCardView *scratchView = [[SEGScratchCardView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            [scratchView updateViewWithDict:dicData callBack:^(NSDictionary *data) {
                
                if ([[data objectForKey:@"avdFlag"] isEqualToString:@"1"]) {
                    
                    NSDictionary *params = @{@"linkURL":[data objectForKey:@"avdUrl"]};
                    UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWebViewLoadController:params];
                    VC.title = @"抽奖";
                    [vc.navigationController pushViewController:VC animated:YES];
                    
                }
                else if ([[data objectForKey:@"avdFlag"] isEqualToString:@"2"]) {
                    //把门禁列表界面消失
                    [vc.navigationController dismissViewControllerAnimated:NO completion:nil];
                    NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[data objectForKey:@"avdUrl"], @"action", [data objectForKey:@"brandName"], @"serviceName",@"1", @"type", nil];
                    [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithAction:dic];
                }
                else {
                    UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWinRecordListViewController:nil];
                    [nav pushViewController:VC animated:YES];
                }
            }];
            [[[UIApplication sharedApplication] keyWindow] addSubview:scratchView];
            break;
        }
        default:
            break;
    }
}

- (void)showRedWalletViewWithType:(SEGRedWalletFromType)type navigation:(UINavigationController *)nav //closeCallback:(SEGAwardRedWalletViewCloseCallBack)closeCallback
{
    // 红包卡卷
    SEGRedWalletView *redWalletView = [[SEGRedWalletView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [redWalletView showRedWalletWithType:type callback:^(SEGAwardRedWalletButtonType buttonType, NSDictionary *awardsResultData) {
        switch (buttonType) {
            case SEGAwardRedWalletButtonType_Open:
            {
                if (type != SEGRedWalletFromType_SignUp) {
                    SEGAwardFromType awardFromType;
                    if (type == SEGRedWalletFromType_FlashBox) {
                        awardFromType = SEGAwardFromType_FlashBox;
                    }else {
                        awardFromType = SEGAwardFromType_OpenDoor;
                    }
                    SEGAwardManager *awardManager = [[SEGAwardManager alloc] initWithAwardType:awardFromType];
                    [awardManager requestAwardsResult:^(id result, NSError *error) {
                        [redWalletView showAwardsResultWithDict:result];
                    }];
                }
                break;
            }
            case SEGAwardRedWalletButtonType_Close:
            {
                if (type == SEGRedWalletFromType_SignUp) {
                    //closeCallback();
                }
                break;
            }
            case SEGAwardRedWalletButtonType_SeeSoon:
            {
                SEGAwardPrizeType prizeType = [[awardsResultData objectForKey:@"prizeType"] intValue];
                switch (prizeType) {
                    case kPrizeTypeProduct: // 实物
                    {
                        if ([[awardsResultData objectForKey:@"avdFlag"] isEqualToString:@"1"]) {
                            //avdFlag:1 后台红包配不出来，判断该模块是否已砍掉，重现不了
                            NSDictionary *params = @{@"linkURL":[awardsResultData objectForKey:@"avdUrl"]};
                            UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWebViewLoadController:params];
                            VC.title = @"抽奖";
                            [nav pushViewController:VC animated:YES];
                            
                        }
                        else if ([[awardsResultData objectForKey:@"avdFlag"] isEqualToString:@"2"]) {
                            
                            NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[awardsResultData objectForKey:@"avdUrl"], @"action", [awardsResultData objectForKey:@"brandName"], @"serviceName",@"1", @"type", nil];
                            [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithAction:dic];
                        }
                        else {
                            UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initWinRecordListViewController:nil];
                            [nav pushViewController:VC animated:YES];
                        }
                        break;
                    }
                    case kPrizeTypeRedWallet://现金红包
                    {
                        NSDictionary *params = @{@"banalceType":@(SEGBanalceType_Cash)};
                        UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initIntegralCashViewController:params];
                        [nav pushViewController:VC animated:YES];
                        break;
                    }
                    case kPrizeTypeCard: // 优惠券
                    {
                        NSDictionary *params = @{@"banalceType":@(SEGBanalceType_Banalce_Cash)};
                        UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initIntegralCashViewController:params];
                        [nav pushViewController:VC animated:YES];
                        break;
                    }
                }
                break;
            }
            case SEGAwardRedWalletButtonType_SeeSoonSignUp:
            {
                NSDictionary *params;
                if ([SEGCheckShareRecordManager sharedManager].redwalletModel.prizeType.integerValue == 10) {
                    params = @{@"banalceType":@(SEGBanalceType_Cash)};
                }else if ([SEGCheckShareRecordManager sharedManager].redwalletModel.prizeType.integerValue == 20) {
                    params = @{@"banalceType":@(SEGBanalceType_Integral)};
                }
                
                UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initIntegralCashViewController:params];
                [nav pushViewController:VC animated:YES];

            }
            default:
                break;
        }
    }];
    [[[UIApplication sharedApplication] keyWindow] addSubview:redWalletView];
}
@end
